<!DOCTYPE html>
<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8"
    contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>配置代码表</title>
<jsp:include page="/common/head.jsp"></jsp:include>
<link href="<%=request.getContextPath()%>/static/plugin/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
<script src="<%=request.getContextPath()%>/static/plugin/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<style>
  #content {
        position: absolute;
        top: 0;
        left: 240px;
        right: 10px;
        bottom: 0;
        box-sizing: border-box;
   }
   ul.ztree {
       width: 230px;
   }  
</style>
</head>
<body class="iframe-page">
    <div class="tree-wrap">
    <div class="tree-title">代码表列表</div>
    <div class="tree-content">
	    <ul id="codeDetailList" class="ztree"></ul>
	</div>
	</div>
	
     <div id="content">
        <iframe id="codeDetailFrame" frameborder="0" style="width:100%;height:100%"></iframe>
     </div>
     <div id="codeDetailMenu" style="width:120px;display:none">
          <div id="delete" iconCls="icon-delete">删除节点</div>
    </div>
     <script>
     var CODEDETAIL_URL = {
          INIT_MAIN: basePath + '/sys/codeDetail/initDetatil',
          INIT_STAT: basePath + '/sys/codeDetail/init',//展示右侧
          DELETE: basePath + '/sys/codeDetail/delete',
          GET_ALL: basePath + '/sys/codeDetail/dataTree',//展示左侧树
          ASYNC_GET_Tree: basePath + '/sys/codeDetail/asyncDataTree'//异步加载左侧树
     };
     var codeDetailFrame = document.getElementById('codeDetailFrame'),
         currentNode = null;
     
     // ztree 初始化设置 
     var setting = {  
         data: {
                simpleData: {
                    enable: true,
                    idKey: 'id',  // code
                    pIdKey: 'parentId' // 父code
                }
            },
			async: {
				enable: true
			},
          callback: {
              onClick: viewCodeDetail,
              onRightClick: showCodeDetailMenu
          }
     },
     ztree;
     
     $(function() {
         // 初始化功能树
         initTree();
         $('#codeDetailMenu').menu({
             onClick: function(item){
                if (item.id == 'add') {
                    addCodeDetail(currentNode);
                } else if (item.id == 'edit') {
                    editCodeDetail(currentNode);
                } else if (item.id == 'delete') {
                    deleteCodeDetail(currentNode);
                }
             }
         }); 
     });
     
     function initTree() {
         //取自URL中
    	 console.log('${param}');
         //取自后台传的变量
    	 console.log('${codeVo}');
         $.ajax({
            url: CODEDETAIL_URL.GET_ALL,
            data: {
            	'parentId': '${param.id}'
            },
            success: function(data) {
            	ztree = $.fn.zTree.init($("#codeDetailList"), setting, data.data);
                expandNode();//展开节点
            }
         });
     }
     

     // 展开树节点
     function expandNode() {
        var nodeId = '${param.extendId}',
            node;
        if(!nodeId){
        	nodeId = '${param.id}';
        }
        if (nodeId) {
            // 获取需要自动展开的节点
            node = ztree.getNodeByParam('id', nodeId, null);
        } else {
            // nodeId为空,展开根节点
            node = ztree.getNodeByParam('parentId', null, null);
        }
        ztree.selectNode(node); //选中节点
        // 如果该节点不包含子节点,则展开其父节点
        if (node &&!node.isParent) {
           node = node.getParentNode(); 
        }
        
        ztree.expandNode(node, true, false, true);
      //初始化右侧iframe内容
        initIframeContent(codeDetailFrame,nodeId);
     }
     
     // 查看资源
     function viewCodeDetail(event, treeId, treeNode) {
        var url = CODEDETAIL_URL.INIT_STAT+'?parentId='+treeNode.id+"&id=${param.id}";
        codeDetailFrame.src = url;
     }
     
     //删除资源
     function deleteCodeDetail(node) { 
        $.sdialog({
            type: 'confirm',
            msg: '您确定要删除<'+node.name+'>节点吗? 删除时会将该节点下的子节点一并删除',
            onConfirm: function(result) {
                if(result){
                 $.sajax({
                        url: CODEDETAIL_URL.DELETE+'?ids='+node.id+'&type=${codeVo.type}',
                        onLoadSuccess: function (data) {
                              $.stip({
                            	  type:'ok',//显示类型 有info、 ok、 danger
               	                  text: '删除成功！',
               	                  onBeforeHide: function () {
               	                	 refreshPage("${param.id}",node.parentId); 
               	                	 initIframeContent(codeDetailFrame, node.parentId);
               	                  }
                              }); 
                        }
                    });
                }
            }
        });
     }
     
     // 显示右键菜单
     function showCodeDetailMenu(event, treeId, treeNode) {
        // 通过treeNode判断是否是在节点上右键
        if (treeNode) {
            currentNode = treeNode;
            //显示和隐藏删除
			var item = $('#codeDetailMenu').menu('getItem', $('#delete')[0]);
            if(!currentNode.parentId) {
                $('#codeDetailMenu').menu('disableItem', item.target);
            } else {
            	$('#codeDetailMenu').menu('enableItem', item.target);
            }
             $('#codeDetailMenu').menu('show',{left:event.pageX, top: event.pageY});  
        }
     }
     
     //页面初始化时，显示iframe内容
     function initIframeContent(iframe,nodeId) {
    	 iframe.src = CODEDETAIL_URL.INIT_STAT+'?parentId='+nodeId+"&id=${param.id}";
     }
     
     // 刷新页面, nodeId为初始化页面时需要自动展开的节点ID
     function refreshPage(extendId) {
    	 var node = ztree.getNodeByParam("id", extendId, null);
         ztree.setting.async.url = CODEDETAIL_URL.ASYNC_GET_Tree + "?parentId=" + extendId;
         node.isParent = true;
         ztree.updateNode(node);
         ztree.reAsyncChildNodes(node, "refresh");
     }
     </script>
</body>
</html>
